<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <!-- 1、引入依赖 -->
  <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

  <div id="app"></div>

  <!-- 2、编写react代码 -->
  <script type="text/babel">

    class App extends React.Component {
      constructor(){
        super()
        this.state = {
          count: 0
        }

      }

      increment1(){
        let { count } = this.state
        count = count + 1
        this.setState({
          count
        })
      }

      decrement1(){
        console.log(this); //undefined
        let { count } = this.state
        count = count - 1
        // React.Component中含有setState方法，继承而来的
        this.setState({
          count
        })
      }


      increment = () => {
        let { count } = this.state
        count = count + 1
        this.setState({
          count
        })
      }

      decrement = () => {
        let { count } = this.state
        count = count - 1
        this.setState({
          count
        })
      }

      // 所有组件必须实现render方法
      render() {
        const { count } = this.state


        // 必须用return返回
        return (
          // {}语法  里面可以是表达式

          <div>
            <div>当前计数：{count}</div>
            <button onClick={this.decrement1.bind(this)}>-1</button>
            <button onClick={this.increment1.bind(this)}>+1</button>

            <button onClick={() => this.decrement1()}>-1</button>
            <button onClick={() => this.increment1()}>+1</button>


            <button onClick={this.decrement}>-1</button>
            <button onClick={this.increment}>+1</button>
          </div>

          // this.decrement1的本质是拿到decrement1函数，将函数传递给react内部，react内部监听到在发生点击的时候，内部回调
          // decrement1函数，react内部调用的时候并不清楚decrement1函数内要绑定的this，相当于decrement1.apply(undefined),没有
          // 正确绑定this，所以在decrement1函数内的this为undefined，不是当前对象
        )
      }
    }


    ReactDOM.render(<App/>, document.getElementById('app')) 
  </script>
</body>
</html>